package game.gui;

/**
 * This class represents the "new game" windows that pop up when a user 
 * wants to start a new game
 * @author Kristen
 */

import game.driver.Controller;
import game.utils.Util;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


//See if the radio buttons need action listeners or not!


public class NewGameGUI  extends JFrame implements ActionListener{
	
	private static final long serialVersionUID = 2645317728409906693L;
	
	Controller controller;
	
	JPanel p1options;
	JPanel p2options;
	JPanel p3options;
	JPanel p4options;
	
	JLabel p1Label;
	JLabel p2Label;
	JLabel p3Label;
	JLabel p4Label;
	JLabel difficulty;
	
	JRadioButton p1comp, p1real;
	JRadioButton p1d1, p1d2, p1d3;
	JRadioButton p2comp, p2real;
	JRadioButton p2d1, p2d2, p2d3;
	JRadioButton p3comp, p3real, p3none;
	JRadioButton p3d1, p3d2, p3d3;
	JRadioButton p4comp, p4real, p4none;
	JRadioButton p4d1, p4d2, p4d3;
	
	JPanel buttonPanel;
	JButton okayButton;
	
	/**
	 * This is the constructor for a new game window. 
	 */
	public NewGameGUI(Controller c){
		super(Util.NEW_GAME);
		controller=c;
		setSize(Util.NEWGAME_WINDOW_WIDTH,Util.NEWGAME_WINDOW_HEIGHT);
		setLayout(new GridLayout(5,2));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
		fill();
	}
	/**
	 * This method begins to add labels and buttons to the
	 * new game window. 
	 */
	public void fill(){
		p1Label=new JLabel(Util.PLAYER_1);
		add(p1Label);
		firstPlayer();
		
		p2Label=new JLabel(Util.PLAYER_2);
		add(p2Label);
		secondPlayer();
		
		p3Label=new JLabel(Util.PLAYER_3);
		add(p3Label);
		thirdPlayer();
		
		p4Label=new JLabel(Util.PLAYER_4);
		add(p4Label);
		fourthPlayer();
		
		
		buttonPanel=new JPanel();
		okayButton=new JButton(Util.OKAY);
		buttonPanel.add(okayButton);
		okayButton.setPreferredSize(new Dimension(80,30));
		okayButton.addActionListener(this);
		add(buttonPanel);
	}
	/**
	 * This method creates a panel of buttons for the first player's options
	 * We do not have 3 difficulties for computer players as we had intended, 
	 * so those options are written as comments.
	 */
	public void firstPlayer(){
		p1options = new JPanel();
		
		p1comp=new JRadioButton(Util.COMPUTER);
		p1real=new JRadioButton(Util.REAL);
		p1d1=new JRadioButton(Util.ONE);
		p1d2=new JRadioButton(Util.TWO);
		p1d3=new JRadioButton(Util.THREE);
		
		ButtonGroup playerTypes=new ButtonGroup();
		playerTypes.add(p1comp);
		playerTypes.add(p1real);
		
		ButtonGroup difficulties=new ButtonGroup();
		difficulties.add(p1d1);
		difficulties.add(p1d2);
		difficulties.add(p1d3);
		
		p1options.add(p1comp);
		p1options.add(p1real);
		difficulty=new JLabel(Util.DIFFICULTY + ": ");
		p1options.add(difficulty);
		p1options.add(p1d1);
		p1options.add(p1d2);
		p1options.add(p1d3);
		
		add(p1options);
	}
	/**
	 * This method creates a panel of buttons for the second player's options
	 * We do not have 3 difficulties for computer players as we had intended, 
	 * so those options are written as comments.
	 */
	public void secondPlayer(){
		p2options = new JPanel();
		
		p2comp=new JRadioButton(Util.COMPUTER);
		p2real=new JRadioButton(Util.REAL);
		p2d1=new JRadioButton(Util.ONE);
		p2d2=new JRadioButton(Util.TWO);
		p2d3=new JRadioButton(Util.THREE);
		
		ButtonGroup playerTypes=new ButtonGroup();
		playerTypes.add(p2comp);
		playerTypes.add(p2real);
		
		ButtonGroup difficulties=new ButtonGroup();
		difficulties.add(p2d1);
		difficulties.add(p2d2);
		difficulties.add(p2d3);
		
		p2options.add(p2comp);
		p2options.add(p2real);
		difficulty=new JLabel(Util.DIFFICULTY + ": ");
		p2options.add(difficulty);
		p2options.add(p2d1);
		p2options.add(p2d2);
		p2options.add(p2d3);
		
		add(p2options);
	}
	/**
	 * This method creates a panel of buttons for the third player's options
	 * We do not have 3 difficulties for computer players as we had intended, 
	 * so those options are written as comments.
	 */
	public void thirdPlayer(){
		p3options = new JPanel();
		
		p3comp=new JRadioButton(Util.COMPUTER);
		p3real=new JRadioButton(Util.REAL);
		p3none=new JRadioButton(Util.NONE);
		p3d1=new JRadioButton(Util.ONE);
		p3d2=new JRadioButton(Util.TWO);
		p3d3=new JRadioButton(Util.THREE);
		
		ButtonGroup playerTypes=new ButtonGroup();
		playerTypes.add(p3comp);
		playerTypes.add(p3real);
		playerTypes.add(p3none);
		
		ButtonGroup difficulties=new ButtonGroup();
		difficulties.add(p3d1);
		difficulties.add(p3d2);
		difficulties.add(p3d3);
		
		p3options.add(p3comp);
		p3options.add(p3real);
		p3options.add(p3none);
		difficulty=new JLabel(Util.DIFFICULTY + ": ");
		p3options.add(difficulty);
		p3options.add(p3d1);
		p3options.add(p3d2);
		p3options.add(p3d3);
		
		add(p3options);
	}
	/**
	 * This method creates a panel of buttons for the fourth player's options
	 * We do not have 3 difficulties for computer players as we had intended, 
	 * so those options are written as comments.
	 */
	public void fourthPlayer(){
		p4options = new JPanel();
		
		p4comp=new JRadioButton(Util.COMPUTER);
		p4real=new JRadioButton(Util.REAL);
		p4none=new JRadioButton(Util.NONE);
		p4d1=new JRadioButton(Util.ONE);
		p4d2=new JRadioButton(Util.TWO);
		p4d3=new JRadioButton(Util.THREE);
		
		ButtonGroup playerTypes=new ButtonGroup();
		playerTypes.add(p4comp);
		playerTypes.add(p4real);
		playerTypes.add(p4none);
		
		ButtonGroup difficulties=new ButtonGroup();
		difficulties.add(p4d1);
		difficulties.add(p4d2);
		difficulties.add(p4d3);
		
		p4options.add(p4comp);
		p4options.add(p4real);
		p4options.add(p4none);
		difficulty=new JLabel(Util.DIFFICULTY + ": ");
		p4options.add(difficulty);
		p4options.add(p4d1);
		p4options.add(p4d2);
		p4options.add(p4d3);
		
		add(p4options);
	}

	/**
	 * This method recognizes when the okay button is pressed on the new game
	 * window. It collects info on what options have been selected and calls
	 * the Constructor's NewGame method to start a game.
	 */
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand()==Util.OKAY){
			
			String[]players;
			int[]isHuman;
			int playerCount=0;
			if(p1comp.isSelected()||p1real.isSelected())
				playerCount++;
			if(p2comp.isSelected()||p2real.isSelected())
				playerCount++;
			if(p3comp.isSelected()||p3real.isSelected())
				playerCount++;
			if(p4comp.isSelected()||p4real.isSelected())
				playerCount++;
			
			players=new String[playerCount];
			isHuman=new int[playerCount];
			
			if(playerCount>0&&p1real.isSelected()){
				players[0]="Player 1";
				isHuman[0]=-1;
			}
			else if(playerCount>0)
			{
				players[0]="Player 1";
				isHuman[0]= determineLevel(p1d1,p1d2,p1d3);
			}
			if(playerCount>1&&p2real.isSelected()){
				players[1]="Player 2";
				isHuman[1]=-1;
			}
			else if(playerCount>1)
			{
				players[1]="Player 2";
				isHuman[1]= determineLevel(p2d1,p2d2,p2d3);
			}
			if(playerCount>2&&p3real.isSelected()){
				players[2]="Player 3";
				isHuman[2]=-1;
			}
			else if(playerCount>2)
			{
				players[2]="Player 3";
				isHuman[2]= determineLevel(p3d1,p3d2,p3d3);
			}
			if(playerCount>3&&p4real.isSelected()){
				players[3]="Player 4";
				isHuman[3]=-1;
			}
			else if(playerCount>3)
			{
				players[3]="Player 4";
				isHuman[3]= determineLevel(p4d1,p4d2,p4d3);
			}
			
			dispose();
			controller.newGame(players,isHuman);			
		}
		
		
	}

	/**
	 * This method determines which level has been chosen for computer players.
	 * @param one, two, and three. The RadioButtons to be checked
	 * @return an int representing the level.
	 */
	public int determineLevel(JRadioButton one, JRadioButton two, JRadioButton three){
		if(three.isSelected())
			return 2;
		if(two.isSelected())
			return 1;
		else
			return 0;
	}
}

